package com.xkdx.caipiao.redpacke;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.hall.HongBaoActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.hongbaolist.HongbaoListAction;
import com.xkdx.caipiao.presistence.hongbaolist.HongbaoListInfo;
import com.xkdx.caipiao.presistence.hongbaolist.HongbaoListModule;
import com.xkdx.caipiao.presistence.hongbaolist.HongbaoListPresistence;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.guangguang.application.MyApplication;

public class BuyRedpacketList extends ThreadActivity {

private ListView lv;

private HongbaoListAction action;
private HongbaoListModule module;
private HongbaoListPresistence presistence;
private String index = "1", size = "10", uid, token;
//private SharePrefenceUtil su;
private ArrayList<HongbaoListInfo> list;
private TextView hongbao;

@Override
protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);
	setContentView(R.layout.redpacket_detail_caipiao);
//	su = new SharePrefenceUtil(getApplicationContext(),
//			SharePrefenceUtil.USERINFO);
	uid = usrSP.getId();
	token = usrSP.getLogintoken();
	findView();
	startThread();
}

@Override
public void onResume(){
	super.onResume();
	if(MyApplication.isClosePacketList){
		MyApplication.isClosePacketList = false;
		finish();
	}
}

private void findView() {
	hongbao = (TextView) findViewById(R.id.top_right_tv);
	hongbao.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			Intent intent = new Intent(BuyRedpacketList.this, HongBaoActivity.class);
			startActivity(intent);
			
		}
	});
	lv = (ListView) findViewById(R.id.lv_list);
	lv.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent = new Intent(BuyRedpacketList.this,
					RedpacketExchange.class);
			intent.putExtra("name", list.get(arg2).getName());
			intent.putExtra("id", list.get(arg2).getId());
			intent.putExtra("money", Float.valueOf(list.get(arg2).getMoney()) / 100+"元");
			
			startActivity(intent);
		}
	});
}

@Override
public void showOnPost(HashMap<String, AbsModule> result) {
	
	if (dataLoadDialog.isShowing()) {
		dataLoadDialog.dismiss();
	}
	
	if (result.containsKey("0")) {
		list = module.list;
		if (list != null && list.size() > 0) {
			HongbaoAdapter adapter = new HongbaoAdapter();
			lv.setAdapter(adapter);
			IConstants.setListViewHeightBasedOnChildren(lv);
		}
	}
}


public class HongbaoAdapter extends BaseAdapter {

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(getApplicationContext())
					.inflate(R.layout.red_packe_list_caipiao, null);

			viewHolder.name = (TextView) convertView
					.findViewById(R.id.tv_title);

			viewHolder.money = (TextView) convertView
					.findViewById(R.id.tv_money);
			viewHolder.time = (TextView) convertView
					.findViewById(R.id.tv_time);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		float mon = Float.valueOf(list.get(position).getMoney()) / 100;
		DecimalFormat df = new DecimalFormat("#0.00");
		String money = df.format(mon);

		viewHolder.name.setText(list.get(position).getName());

		viewHolder.time.setText(list.get(position).getValidity()+"天");

		viewHolder.money.setText(mon+"元");

		return convertView;
	}

	class ViewHolder {

		TextView name, time, money;

	}

}

@Override
public void startThread() {
	if (dataLoadDialog.isShowing()) {
		dataLoadDialog.dismiss();
	}
	action = new HongbaoListAction(uid, token, index, size);
	module = new HongbaoListModule();
	presistence = new HongbaoListPresistence(BuyRedpacketList.this);
	presistence.setActitons(action);
	presistence.setModule(module);
	presistence.execute();
}

@Override
public void onBackClick(View view) {
	finish();

}

}
